<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Exhibition extends BaseModel
{
    use HasFactory;

    const CREATED_AT='create_time';
    const UPDATED_AT='change_time';

    protected $table = 'exhibition';


    /**
     * 获取关联到前言
     */
    public function foreword()
    {
        return $this->belongsTo('App\Models\Foreword' , 'fore_id');
    }


     /**
     * 关联作品
     */
    public function productions()
    {
        return $this->belongsToMany('App\Models\Production' , 'exhibition_production' , 'exhibition_id' , 'production_id');
    }

    /**
     * 关联机柜
     */
    public function bookcases()
    {
        return $this->belongsToMany('App\Models\Bookcase' , 'bookcase_exhibition' , 'exhibition_id' , 'bookcase_id');
    }



    /**
     *  展览添加
     * @param $data 添加的数据
     */
    public function add($data){
        $this->name = $data->name;
        $this->type_name = $data->type_name;
        $this->address = $data->address;
        $this->start_time = $data->start_time;
        $this->end_time = $data->end_time;
        $this->opening_time = $data->opening_time;
        $this->roll_speed = $data->roll_speed;
        $this->page_stay_min_time = $data->page_stay_min_time;
        $this->sponsor = $data->sponsor;
        $this->undertaker = $data->undertaker;
        $this->special_thanks = $data->special_thanks;
        $this->academic_host = $data->academic_host;
        $this->chief_inspector = $data->chief_inspector;
        $this->curator = $data->curator;
        $this->intro = $data->intro;
        $this->fore_id = $data->fore_id;
        $this->manage_id = $data->manage_id;
        $this->is_del = 1;

        return  $this->save();
    }

    /**
     *  展览修改
     * @param $data 添加的数据
     */
    public function change($data){
        $res = $this->where('is_del' , 1)->find($data['id']);
        if(!$res){
            throw new Exception('数据获取失败');
        }

        $res->name = $data->name;
        $res->type_name = $data->type_name;
        $res->address = $data->address;
        $res->start_time = $data->start_time;
        $res->end_time = $data->end_time;
        $res->opening_time = $data->opening_time;
        $res->roll_speed = $data->roll_speed;
        $res->page_stay_min_time = $data->page_stay_min_time;
        $res->sponsor = $data->sponsor;
        $res->undertaker = $data->undertaker;
        $res->special_thanks = $data->special_thanks;
        $res->academic_host = $data->academic_host;
        $res->chief_inspector = $data->chief_inspector;
        $res->curator = $data->curator;
        $res->intro = $data->intro;
        $res->fore_id = $data->fore_id;
        return  $res->save();
    }


    /**
     * 判断展览名称是否已经存在
     * @param name 展览名称
     * @param id 展览id   可选，主要是用于修改
     */
    public function exhibitionNameIsExists($name , $id = null){
        $res = $this->where('name' , $name)->where(function($query) use($id){
                if(!empty($id)){
                    $query->where('id' , '<>' , $id);
                }
        })->where('is_del' , 1)->first();
        return $res;
    }


    /**
     * 组装返回数据
     * @param $data
     */
    public function exhibitionData($data){
        $result = [];
        $i = 0;
        foreach($data as $key=>&$val){
            if (!empty($val['foreword'])) {
                if (empty($val['foreword']['roll_speed'])) {
                    $val['foreword']['roll_speed'] = $val['roll_speed'];
                }
                if (empty($val['foreword']['page_stay_min_time'])) {
                    $val['foreword']['page_stay_min_time'] = $val['page_stay_min_time'];
                }
                if (!empty($val['foreword']['img'])) {
                    $file_name = explode('/', $val['foreword']['img']);
                    $file_name = end($file_name);

                    $val['foreword']['file_name'] = $file_name;

                    $result['img_all'][$i]['file_name'] = $file_name;
                    $result['img_all'][$i]['img'] = $val['foreword']['img'];
                    $i++;
                } else {
                    $val['foreword']['file_name'] = '';
                }
            }else{
                $val['foreword'] = null;
            }

            foreach($val['production_info'] as $k=>&$v){
                if(empty($v['roll_speed'])){
                    $v['roll_speed'] = $val['roll_speed'];
                }
                if(empty($v['page_stay_min_time'])){
                    $v['page_stay_min_time'] = $val['page_stay_min_time'];
                }

                $file_name = explode('/',$v['img']);
                $file_name = end($file_name);
                $v['file_name'] = $file_name;

                $result['img_all'][$i]['file_name'] = $file_name;
                $result['img_all'][$i]['img'] = $v['img'];
                $i++;
            }
            $result['data'][$key] = $val;
        }

        return $result;
    }


 



}
